package com.example.musicserver.controller;

import com.example.musicserver.common.Constant;
import com.example.musicserver.common.Response;
import com.example.musicserver.mapper.LoveMusicMapper;
import com.example.musicserver.model.Music;
import com.example.musicserver.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/lovemusic")
@RestController
public class LoveMusicController {

    @Autowired
    private LoveMusicMapper loveMusicMapper;

    /**
     * 点赞收藏音乐
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("/likemusic")
    public Response<Boolean> likeMusic(@RequestParam String id, HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        // 1. 检查是否登录了
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constant.USER_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new Response<>(-1, "请登录之后再进行收藏！", false);
        }
        User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
        int userId = user.getId();
        Music music = loveMusicMapper.findLoveMusicByMusicIdAndUserId(userId,musicId);

        if(music != null){
            return new Response<>(-1,"该音乐已收藏",false);
        }

        boolean ret = loveMusicMapper.insertIntoLoveMusic(userId,musicId);
        if(ret){
            return new Response<>(1,"收藏成功！",true);
        }else{
            return new Response<>(-1,"该音乐已收藏！",false);
        }
    }

    /**
     * 查询用户收藏的音乐
     * @param musicName
     * @param request
     * @return
     */
    @RequestMapping("/findlikemusic")
    public Response<List<Music>> findLikeMusic(@RequestParam(required = false) String musicName,HttpServletRequest request){
        // 1. 检查是否登录了
        HttpSession session = request.getSession(false);
        if (session == null || session.getAttribute(Constant.USER_SESSION_KEY) == null) {
            System.out.println("没有登录！");
            return new Response<>(-1, "请登录之后再进行收藏！", null);
        }
        User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
        int userId = user.getId();

        List<Music> musicList = null;
        if(musicName == null){
            musicList = loveMusicMapper.findLoveMusicByUserId(userId);
        }else{
            musicList = loveMusicMapper.findLoveMusicBykeyAndUID(userId,musicName);
        }
        return new Response<>(1,"查询到了所有的歌曲信息",musicList);
    }

    @RequestMapping("/deletelikemusic")
    public Response<Boolean> deleteLikeMusic(@RequestParam String id, HttpServletRequest request){
        int musicId = Integer.parseInt(id);
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(Constant.USER_SESSION_KEY);
        int userId = user.getId();

        boolean ret = loveMusicMapper.deleteLikeMusic(userId,musicId);
        if(ret){
            return new Response<>(1,"取消收藏成功！",true);
        }else{
            return new Response<>(-1,"取消收藏失败！",true);
        }
    }

}
